<!DOCTYPE html>

<html>
<head>
  <title>dataParser.coffee</title>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <meta name="viewport" content="width=device-width, target-densitydpi=160dpi, initial-scale=1.0; maximum-scale=1.0; user-scalable=0;">
  <link rel="stylesheet" media="all" href="docco.css" />
</head>
<body>
  <div id="container">
    <div id="background"></div>
    
      <ul id="jump_to">
        <li>
          <a class="large" href="javascript:void(0);">Jump To &hellip;</a>
          <a class="small" href="javascript:void(0);">+</a>
          <div id="jump_wrapper">
          <div id="jump_page">
            
              
              <a class="source" href="configure.html">
                configure.coffee
              </a>
            
              
              <a class="source" href="dataParser.html">
                dataParser.coffee
              </a>
            
              
              <a class="source" href="database.html">
                database.coffee
              </a>
            
              
              <a class="source" href="logger.html">
                logger.coffee
              </a>
            
              
              <a class="source" href="mail.html">
                mail.coffee
              </a>
            
              
              <a class="source" href="messages.html">
                messages.coffee
              </a>
            
              
              <a class="source" href="routes.html">
                routes.coffee
              </a>
            
              
              <a class="source" href="settings.html">
                settings.coffee
              </a>
            
              
              <a class="source" href="settings.default.html">
                settings.default.coffee
              </a>
            
              
              <a class="source" href="twitter.html">
                twitter.coffee
              </a>
            
          </div>
        </li>
      </ul>
    
    <ul class="sections">
        
          <li id="title">
              <div class="annotation">
                  <h1>dataParser.coffee</h1>
              </div>
          </li>
        
        
        
        <li id="section-1">
            <div class="annotation">
              
              <div class="pilwrap for-h2">
                <a class="pilcrow" href="#section-1">&#182;</a>
              </div>
              <h2>SERVER DATA PARSER</h2>

            </div>
            
        </li>
        
        
        <li id="section-2">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-2">&#182;</a>
              </div>
              <p>Parse and transform data.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre><span class="class"><span class="keyword">class</span> <span class="title">DataParser</span></span>

    logger = require <span class="string">"./logger.coffee"</span>
    moment = require <span class="string">"moment"</span>
    settings = require <span class="string">"./settings.coffee"</span></pre></div></div>
            
        </li>
        
        
        <li id="section-3">
            <div class="annotation">
              
              <div class="pilwrap for-h2">
                <a class="pilcrow" href="#section-3">&#182;</a>
              </div>
              <h2>DATE PARSER</h2>

            </div>
            
        </li>
        
        
        <li id="section-4">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-4">&#182;</a>
              </div>
              <p>Try to convert a string to a valid date and time. Return false if no date was found,
or a moment object with the date if parsing is successful.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>    <span class="function"><span class="title">stringToDate</span></span> = (text) -&gt;
        <span class="keyword">if</span> <span class="keyword">not</span> text? <span class="keyword">or</span> text <span class="keyword">is</span> <span class="string">""</span>
            <span class="keyword">if</span> settings.General.debug
                logger.info <span class="string">"DataParser.stringToDate"</span>, <span class="string">"Text is empty. Abort!"</span>
            <span class="keyword">return</span> <span class="literal">false</span></pre></div></div>
            
        </li>
        
        
        <li id="section-5">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-5">&#182;</a>
              </div>
              <p>Check if date should be added or subtracted.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>        <span class="function"><span class="title">applyLastNext</span></span> = (type, range, modifier) -&gt;
            day = days[range]

            <span class="keyword">if</span> day?
                diff = day - date.getDay()

                <span class="keyword">if</span> diff <span class="keyword">is</span> <span class="number">0</span>
                    diff = <span class="number">7</span> * modifier
                <span class="keyword">else</span> <span class="keyword">if</span> diff &gt; <span class="number">0</span> <span class="keyword">and</span> type <span class="keyword">is</span> <span class="string">"last"</span>
                    diff -= <span class="number">7</span>
                <span class="keyword">else</span> <span class="keyword">if</span> diff &lt; <span class="number">0</span> <span class="keyword">and</span> type <span class="keyword">is</span> <span class="string">"next"</span>
                    diff += <span class="number">7</span>

                date.setDate date.getDate() + diff</pre></div></div>
            
        </li>
        
        
        <li id="section-6">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-6">&#182;</a>
              </div>
              <p>Process the value to get &quot;next&quot; and &quot;last&quot; date formats.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>        <span class="function"><span class="title">process</span></span> = (val) -&gt;
            split = val.split <span class="string">" "</span>
            type = split[<span class="number">0</span>]
            range = split[<span class="number">1</span>].substring <span class="number">0</span>, <span class="number">3</span>
            typeIsNumber = <span class="regexp">/\d+/</span>.test(type)
            ago = split[<span class="number">2</span>] <span class="keyword">is</span> <span class="string">"ago"</span>

            num = ((<span class="keyword">if</span> type <span class="keyword">is</span> <span class="string">"last"</span> <span class="keyword">then</span> -<span class="number">1</span> <span class="keyword">else</span> <span class="number">1</span>)) * ((<span class="keyword">if</span> ago <span class="keyword">then</span> -<span class="number">1</span> <span class="keyword">else</span> <span class="number">1</span>))

            <span class="keyword">if</span> typeIsNumber
                num *= parseInt(type, <span class="number">10</span>)

            <span class="keyword">if</span> ranges.hasOwnProperty range
                <span class="keyword">return</span> date[<span class="string">"set"</span> + ranges[range]](date[<span class="string">"get"</span> + ranges[range]]() + num)
            <span class="keyword">else</span> <span class="keyword">if</span> range <span class="keyword">is</span> <span class="string">"wee"</span>
                <span class="keyword">return</span> date.setDate(date.getDate() + (num * <span class="number">7</span>))

            <span class="keyword">if</span> type <span class="keyword">is</span> <span class="string">"next"</span> <span class="keyword">or</span> type <span class="keyword">is</span> <span class="string">"last"</span>
                applyLastNext type, range, num
            <span class="keyword">else</span> <span class="keyword">if</span> <span class="keyword">not</span> typeIsNumber
                <span class="keyword">return</span> <span class="literal">false</span>

            <span class="keyword">return</span> <span class="literal">true</span></pre></div></div>
            
        </li>
        
        
        <li id="section-7">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-7">&#182;</a>
              </div>
              <p>Trim and prepare the text.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>        text = text.trim().replace(<span class="regexp">/\s{2,}/g, " ").replace(/[\t\r\n]/g</span>, <span class="string">""</span>).toLowerCase()</pre></div></div>
            
        </li>
        
        
        <li id="section-8">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-8">&#182;</a>
              </div>
              <p>Parse now.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>        <span class="keyword">if</span> text <span class="keyword">is</span> <span class="string">"now"</span>
            <span class="keyword">return</span> moment()</pre></div></div>
            
        </li>
        
        
        <li id="section-9">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-9">&#182;</a>
              </div>
              <p>Parse tomorrow.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>        <span class="keyword">if</span> text <span class="keyword">is</span> <span class="string">"tomorrow"</span>
            <span class="keyword">return</span> moment().add <span class="string">"d"</span>, <span class="number">1</span></pre></div></div>
            
        </li>
        
        
        <li id="section-10">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-10">&#182;</a>
              </div>
              <p>Parse yesterday.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>        <span class="keyword">if</span> text <span class="keyword">is</span> <span class="string">"yesterday"</span>
            <span class="keyword">return</span> moment().subtract <span class="string">"d"</span>, <span class="number">1</span></pre></div></div>
            
        </li>
        
        
        <li id="section-11">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-11">&#182;</a>
              </div>
              <p>Find matched text.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>        match = text.match(<span class="regexp">/^(\d{2,4})-(\d{2})-(\d{2})(?:\s(\d{1,2}):(\d{2})(?::\d{2})?)?(?:\.(\d+)?)?$/</span>)

        <span class="keyword">if</span> match
            year = (<span class="keyword">if</span> match[<span class="number">1</span>] &gt;= <span class="number">0</span> <span class="keyword">and</span> match[<span class="number">1</span>] &lt;= <span class="number">69</span> <span class="keyword">then</span> +match[<span class="number">1</span>] + <span class="number">2000</span> <span class="keyword">else</span> match[<span class="number">1</span>])
            date = <span class="keyword">new</span> Date(year, parseInt(match[<span class="number">2</span>], <span class="number">10</span>) - <span class="number">1</span>, match[<span class="number">3</span>], match[<span class="number">4</span>] <span class="keyword">or</span> <span class="number">0</span>, match[<span class="number">5</span>] <span class="keyword">or</span> <span class="number">0</span>, match[<span class="number">6</span>] <span class="keyword">or</span> <span class="number">0</span>, match[<span class="number">7</span>] <span class="keyword">or</span> <span class="number">0</span>)
            <span class="keyword">return</span> moment date

        date = <span class="keyword">new</span> Date()</pre></div></div>
            
        </li>
        
        
        <li id="section-12">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-12">&#182;</a>
              </div>
              <p>Define specific dates.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>        days =
            sun: <span class="number">0</span>
            mon: <span class="number">1</span>
            tue: <span class="number">2</span>
            wed: <span class="number">3</span>
            thu: <span class="number">4</span>
            fri: <span class="number">5</span>
            sat: <span class="number">6</span></pre></div></div>
            
        </li>
        
        
        <li id="section-13">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-13">&#182;</a>
              </div>
              <p>Define specific ranges.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>        ranges =
            yea: <span class="string">"FullYear"</span>
            mon: <span class="string">"Month"</span>
            day: <span class="string">"Date"</span>
            hou: <span class="string">"Hours"</span>
            min: <span class="string">"Minutes"</span>
            sec: <span class="string">"Seconds"</span>

        regex = <span class="string">"([+-]?\\d+\\s"</span> + <span class="string">"(years?|months?|weeks?|days?|hours?|min|minutes?|sec|seconds?"</span> + <span class="string">"|sun\\.?|sunday|mon\\.?|monday|tue\\.?|tuesday|wed\\.?|wednesday"</span> + <span class="string">"|thu\\.?|thursday|fri\\.?|friday|sat\\.?|saturday)|(last|next)\\s"</span> + <span class="string">"(years?|months?|weeks?|days?|hours?|min|minutes?|sec|seconds?"</span> + <span class="string">"|sun\\.?|sunday|mon\\.?|monday|tue\\.?|tuesday|wed\\.?|wednesday"</span> + <span class="string">"|thu\\.?|thursday|fri\\.?|friday|sat\\.?|saturday))(\\sago)?"</span>
        match = text.match(<span class="keyword">new</span> RegExp(regex, <span class="string">"gi"</span>))</pre></div></div>
            
        </li>
        
        
        <li id="section-14">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-14">&#182;</a>
              </div>
              <p>Return false if no matched value was found.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>        <span class="keyword">return</span> <span class="literal">false</span> <span class="keyword">unless</span> match

        i = <span class="number">0</span>
        len = match.length

        <span class="keyword">while</span> i &lt; len
            <span class="keyword">return</span> <span class="literal">false</span> <span class="keyword">unless</span> process(match[i])
            i++</pre></div></div>
            
        </li>
        
        
        <li id="section-15">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-15">&#182;</a>
              </div>
              <p>Return a moment object with the parsed date.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>        <span class="keyword">return</span> moment date</pre></div></div>
            
        </li>
        
        
        <li id="section-16">
            <div class="annotation">
              
              <div class="pilwrap for-h2">
                <a class="pilcrow" href="#section-16">&#182;</a>
              </div>
              <h2>HTML PARSER</h2>

            </div>
            
        </li>
        
        
        <li id="section-17">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-17">&#182;</a>
              </div>
              <p>Replace all links on text with proper A HREF tags.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>    htmlLinkify: (inputText) =&gt;
        <span class="keyword">return</span> <span class="string">""</span> <span class="keyword">if</span> <span class="keyword">not</span> inputText? <span class="keyword">or</span> inputText <span class="keyword">is</span> <span class="string">""</span>
        result = inputText</pre></div></div>
            
        </li>
        
        
        <li id="section-18">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-18">&#182;</a>
              </div>
              <p>URLs starting with http://, https:// or ftp://</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>        pattern = <span class="regexp">/(\b(https?|ftp):\/\/[-A-Z0-9+&amp;@#\/%?=~_|!:,.;]*[-A-Z0-9+&amp;@#\/%=~_|])/gim</span>
        result = result.replace pattern, <span class="string">"&lt;a href=\"$1\"&gt;$1&lt;/a&gt;"</span></pre></div></div>
            
        </li>
        
        
        <li id="section-19">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-19">&#182;</a>
              </div>
              <p>URLs starting with &quot;www.&quot;</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>        pattern = <span class="regexp">/(^|[^\/])(www\.[\S]+(\b|$))/gim</span>
        result = result.replace pattern, <span class="string">"$1&lt;a href=\"http://$2\"&gt;$2&lt;/a&gt;"</span>

        <span class="keyword">return</span> result</pre></div></div>
            
        </li>
        
        
        <li id="section-20">
            <div class="annotation">
              
              <div class="pilwrap for-h2">
                <a class="pilcrow" href="#section-20">&#182;</a>
              </div>
              <h2>COUNTDOWN PARSER</h2>

            </div>
            
        </li>
        
        
        <li id="section-21">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-21">&#182;</a>
              </div>
              <p>Parse a text and append its values to the passed <code>cd</code> countdown model.
This is used mainly to transform inbound emails and twitter messages to countdown.
Will return true only if a valid and future target date was found.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>    textToCountdown: (cd, text) =&gt;
        <span class="keyword">if</span> <span class="keyword">not</span> text? <span class="keyword">or</span> text <span class="keyword">is</span> <span class="string">""</span>
            <span class="keyword">if</span> settings.General.debug
                logger.info <span class="string">"DataParser.textToCountdown"</span>, <span class="string">"Text is empty. Abort!"</span>, cd?.id
            <span class="keyword">return</span>

        now = moment()
        futureDate = <span class="literal">false</span>
        foundDate = <span class="literal">false</span>
        foundPlaySound = <span class="literal">false</span>
        onEndMessage = <span class="string">""</span></pre></div></div>
            
        </li>
        
        
        <li id="section-22">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-22">&#182;</a>
              </div>
              <p>Split text in lines.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>        lines = text.split <span class="string">"\n"</span></pre></div></div>
            
        </li>
        
        
        <li id="section-23">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-23">&#182;</a>
              </div>
              <p>Parse each line of the text.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>        <span class="keyword">for</span> line <span class="keyword">in</span> lines
            <span class="keyword">if</span> <span class="keyword">not</span> foundDate
                date = stringToDate line</pre></div></div>
            
        </li>
        
        
        <li id="section-24">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-24">&#182;</a>
              </div>
              <p>If a valid date is found, set the <code>targetDate</code>, otherwise
append text to the <code>onEndMessage</code> property.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>                <span class="keyword">if</span> date <span class="keyword">isnt</span> <span class="literal">false</span>
                    foundDate = <span class="literal">true</span>
                    futureDate = date.isAfter now
                    cd.set <span class="string">"targetDate"</span>, date
                <span class="keyword">else</span>
                    lc = line.toLowerCase()</pre></div></div>
            
        </li>
        
        
        <li id="section-25">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-25">&#182;</a>
              </div>
              <p>Check if line represent a <code>playSound</code> setting.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>                    <span class="keyword">if</span> <span class="keyword">not</span> foundPlaySound <span class="keyword">and</span> lc.indexOf(<span class="string">"playsound"</span>) &gt;= <span class="number">0</span> <span class="keyword">or</span> lc.indexOf(<span class="string">"play sound"</span>) &gt;= <span class="number">0</span>
                        <span class="keyword">if</span> lc.indexOf(<span class="string">"not"</span>) &gt;= <span class="number">0</span> <span class="keyword">or</span> lc.indexOf(<span class="string">"don't"</span>) &gt;= <span class="number">0</span>
                            foundPlaySound = <span class="literal">true</span>
                            cd.set <span class="string">"playSound"</span>, <span class="literal">false</span></pre></div></div>
            
        </li>
        
        
        <li id="section-26">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-26">&#182;</a>
              </div>
              <p>Nothing special found, so append the text to the <code>onEndMessage</code>.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>                    <span class="keyword">else</span>
                        onEndMessage += line + <span class="string">" "</span></pre></div></div>
            
        </li>
        
        
        <li id="section-27">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-27">&#182;</a>
              </div>
              <p>Set the countdown&#39;s <code>onEndMessage</code>.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>        cd.set <span class="string">"onEndMessage"</span>, onEndMessage</pre></div></div>
            
        </li>
        
        
        <li id="section-28">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-28">&#182;</a>
              </div>
              <p>Return true if a date was found and it&#39;s a future date.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>        <span class="keyword">return</span> foundDate <span class="keyword">and</span> futureDate</pre></div></div>
            
        </li>
        
        
        <li id="section-29">
            <div class="annotation">
              
              <div class="pilwrap for-h2">
                <a class="pilcrow" href="#section-29">&#182;</a>
              </div>
              <h2>Singleton implementation</h2>

            </div>
            
        </li>
        
        
        <li id="section-30">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-30">&#182;</a>
              </div>
              
            </div>
            
            <div class="content"><div class='highlight'><pre>DataParser.<span class="function"><span class="title">getInstance</span></span> = -&gt;
    <span class="property">@instance</span> = <span class="keyword">new</span> DataParser() <span class="keyword">if</span> <span class="keyword">not</span> <span class="property">@instance</span>?
    <span class="keyword">return</span> <span class="property">@instance</span>

module.exports = exports = DataParser.getInstance()</pre></div></div>
            
        </li>
        
    </ul>
  </div>
</body>
</html>
